{% extends 'base.html' %}

{% block title %}重置密码 - {{ user_obj.username }} - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
                <li class="breadcrumb-item"><a href="{% url 'assets:user_list' %}">用户管理</a></li>
                <li class="breadcrumb-item"><a href="{% url 'assets:user_detail' user_obj.pk %}">{{ user_obj.username }}</a></li>
                <li class="breadcrumb-item active" aria-current="page">重置密码</li>
            </ol>
        </nav>
        
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2><i class="fas fa-key me-2"></i>重置用户密码</h2>
            <a href="{% url 'assets:user_detail' user_obj.pk %}" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left me-1"></i>返回用户详情
            </a>
        </div>

        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">
                            <i class="fas fa-user me-2"></i>{{ user_obj.get_full_name|default:user_obj.username }}
                        </h5>
                    </div>
                    <div class="card-body">
                        <!-- 用户信息展示 -->
                        <div class="alert alert-info">
                            <div class="d-flex align-items-center">
                                <i class="fas fa-info-circle fa-2x me-3"></i>
                                <div>
                                    <h6 class="mb-1">即将重置以下用户的密码:</h6>
                                    <p class="mb-1"><strong>用户名:</strong> {{ user_obj.username }}</p>
                                    {% if user_obj.email %}
                                    <p class="mb-0"><strong>邮箱:</strong> {{ user_obj.email }}</p>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <form method="post">
                            {% csrf_token %}
                            
                            <div class="mb-3">
                                <label for="new_password" class="form-label">
                                    <i class="fas fa-lock me-1"></i>新密码 <span class="text-danger">*</span>
                                </label>
                                <div class="input-group">
                                    <input type="password" class="form-control" id="new_password" name="new_password" required minlength="6">
                                    <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('new_password')">
                                        <i class="fas fa-eye" id="new_password_icon"></i>
                                    </button>
                                </div>
                                <div class="form-text">密码长度至少6位</div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="confirm_password" class="form-label">
                                    <i class="fas fa-lock me-1"></i>确认密码 <span class="text-danger">*</span>
                                </label>
                                <div class="input-group">
                                    <input type="password" class="form-control" id="confirm_password" name="confirm_password" required minlength="6">
                                    <button class="btn btn-outline-secondary" type="button" onclick="togglePassword('confirm_password')">
                                        <i class="fas fa-eye" id="confirm_password_icon"></i>
                                    </button>
                                </div>
                                <div class="form-text">请再次输入新密码</div>
                            </div>

                            <!-- 密码强度提示 -->
                            <div class="alert alert-warning">
                                <h6><i class="fas fa-shield-alt me-2"></i>密码安全建议:</h6>
                                <ul class="mb-0">
                                    <li>密码长度至少6位</li>
                                    <li>建议包含大小写字母、数字和特殊字符</li>
                                    <li>避免使用常见密码或个人信息</li>
                                    <li>定期更换密码</li>
                                </ul>
                            </div>
                            
                            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                                <a href="{% url 'assets:user_detail' user_obj.pk %}" class="btn btn-secondary me-md-2">
                                    <i class="fas fa-times me-1"></i>取消
                                </a>
                                <button type="submit" class="btn btn-warning" onclick="return confirmReset()">
                                    <i class="fas fa-key me-1"></i>重置密码
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function togglePassword(fieldId) {
    const field = document.getElementById(fieldId);
    const icon = document.getElementById(fieldId + '_icon');
    
    if (field.type === 'password') {
        field.type = 'text';
        icon.className = 'fas fa-eye-slash';
    } else {
        field.type = 'password';
        icon.className = 'fas fa-eye';
    }
}

function confirmReset() {
    const newPassword = document.getElementById('new_password').value;
    const confirmPassword = document.getElementById('confirm_password').value;
    const username = '{{ user_obj.username }}';
    
    if (!newPassword || !confirmPassword) {
        alert('请填写完整的密码信息');
        return false;
    }
    
    if (newPassword.length < 6) {
        alert('密码长度不能少于6位');
        return false;
    }
    
    if (newPassword !== confirmPassword) {
        alert('两次输入的密码不一致');
        return false;
    }
    
    return confirm(`确定要重置用户 "${username}" 的密码吗？\n\n重置后用户需要使用新密码登录。`);
}

// 实时密码匹配检查
document.addEventListener('DOMContentLoaded', function() {
    const newPassword = document.getElementById('new_password');
    const confirmPassword = document.getElementById('confirm_password');
    
    function checkPasswordMatch() {
        if (confirmPassword.value && newPassword.value !== confirmPassword.value) {
            confirmPassword.setCustomValidity('密码不匹配');
            confirmPassword.classList.add('is-invalid');
        } else {
            confirmPassword.setCustomValidity('');
            confirmPassword.classList.remove('is-invalid');
        }
    }
    
    newPassword.addEventListener('input', checkPasswordMatch);
    confirmPassword.addEventListener('input', checkPasswordMatch);
});
</script>
{% endblock %}